Skip to content

fix(service-ai): estimate non-zero token usage in MemoryLLMAdapter#2000

Merged
os-zhuang merged 1 commit into
mainfrom
fix/memory-adapter-token-usage
Jun 17, 2026
Merged

fix(service-ai): estimate non-zero token usage in MemoryLLMAdapter#2000
os-zhuang merged 1 commit into
mainfrom
fix/memory-adapter-token-usage

Conversation

@os-zhuang

Copy link
Copy Markdown
Contributor

Problem

MemoryLLMAdapter is the money-free stand-in for a real LLM in dev, CI, and local E2E. But it hardcoded usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 } on every return path (chat, complete, streamChat finish, generateObject, tool-call and error branches).

Consequence: any feature that meters tokens — cloud's AI quota guardrail, free-tier lifetime caps, paid monthly hard cap, usage dashboards, cost self-stop — could not be exercised end-to-end without a paid provider key. You literally had to spend real API money to test a cost-control feature.

Fix

Estimate usage crudely from the prompt messages + echoed output (~4 chars/token, the standard ballpark) and return it on all paths via two small helpers (estimateTokens / estimateUsage).

This is intentionally not provider-accurate — it only needs to be non-zero, additive (total = prompt + completion), and grow with input size, so usage-driven behaviour is testable money-free.

Test

Adds a unit test asserting the new usage is non-zero, additive, and monotonic in input size. Full file: 103/103 pass.

Verification

Confirmed live in local E2E: assistant ai_messages rows now meter ~5000 tokens each (previously 0), which lets the cloud token guardrail actually accumulate toward its caps using the echo adapter.

🤖 Generated with Claude Code

The in-memory adapter stands in for a real LLM in dev/CI/local-E2E, but
returned a flat `usage: {0,0,0}` on every path. That made every
token-metering feature — quota guardrails, lifetime caps, usage
dashboards, cost stops — impossible to exercise without a paid provider
key, i.e. without spending real money.

Estimate usage crudely (~4 chars/token, the standard ballpark) from the
prompt messages and echoed output across chat/complete/streamChat/
generateObject. Intentionally NOT provider-accurate — it exists so
usage-driven behaviour is testable money-free, not to bill anyone.

Adds a unit test asserting non-zero, additive (total = prompt+completion),
monotonic-in-input-size usage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jun 17, 2026 3:38am

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): packages/services.

5 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:

  • content/docs/guides/packages.mdx (via packages/services)
  • content/docs/guides/runtime-services/audit-service.mdx (via packages/services)
  • content/docs/guides/runtime-services/index.mdx (via packages/services)
  • content/docs/guides/runtime-services/settings-service.mdx (via packages/services)
  • content/docs/protocol/objectos/i18n-standard.mdx (via packages/services)

Advisory only. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs origin/main → pass the list as args.docs.

@os-zhuang
os-zhuang merged commit 3029236 into main Jun 17, 2026
14 checks passed
@os-zhuang
os-zhuang deleted the fix/memory-adapter-token-usage branch June 17, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant